import {Effect, Reducer} from 'umi';
import {getWidgetByProjectId} from "@/services/analysis";

export interface WidgetIndexModelState {
  widgetList: any[];
}

export interface WidgetIndexModelType {
  namespace: 'widgetIndex';
  state: WidgetIndexModelState;
  effects: {
    fetchWidgets: Effect;

  };
  reducers: {
    save: Reducer;
  };
}

const WidgetIndexModel: WidgetIndexModelType = {
  namespace: 'widgetIndex',

  state: {
    widgetList: [],
  },

  effects: {
    // 获取显示的指标详情
    * fetchWidgets({payload, callback}: any, {put, call}: any) {
      const response = yield call(getWidgetByProjectId, payload);
      if (callback) callback(response);

      if (response && response.payload) {
        // 数据表列表
        yield put({
          type: 'save',
          payload: {widgetList: response.payload},
        });
      }
    },

  },

  reducers: {
    save(state, {payload}) {
      return {
        ...state,
        ...payload,
      };
    },
  },
};

export default WidgetIndexModel;
